package org.jeecg.modules.basic.attendance.controller;

import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.basic.attendance.entity.LwgyInoutAttendance;
import org.jeecg.modules.basic.attendance.entity.LwgyInoutAttendanceFormStu;
import org.jeecg.modules.basic.attendance.service.ILwgyInoutAttendanceService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.basic.demo.apartment.entity.LwgyBaseApartment;
import org.jeecg.modules.basic.demo.apartment.service.ILwgyBaseApartmentService;
import org.jeecg.modules.basic.demo.bed.entity.LwgyBaseBed;
import org.jeecg.modules.basic.demo.bed.mapper.LwgyBaseBedMapper;
import org.jeecg.modules.basic.demo.bed.service.ILwgyBaseBedService;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 考勤记录
 * @Author: jeecg-boot
 * @Date:   2024-06-03
 * @Version: V1.0
 */
@Api(tags="考勤记录")
@RestController
@RequestMapping("/attendance/lwgyInoutAttendance")
@Slf4j
public class LwgyInoutAttendanceController extends JeecgController<LwgyInoutAttendance, ILwgyInoutAttendanceService> {
	@Autowired
	private ILwgyInoutAttendanceService lwgyInoutAttendanceService;
	 @Autowired
	 private ILwgyBaseApartmentService lwgyBaseApartmentService;
	 @Autowired
	 private ILwgyBaseBedService lwgyBaseBedService;
	
	/**
	 * 分页列表查询
	 *
	 * @param lwgyInoutAttendance
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "考勤记录-分页列表查询")
	@ApiOperation(value="考勤记录-分页列表查询", notes="考勤记录-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(LwgyInoutAttendance lwgyInoutAttendance,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<LwgyInoutAttendance> queryWrapper = QueryGenerator.initQueryWrapper(lwgyInoutAttendance, req.getParameterMap());
		Page<LwgyInoutAttendance> page = new Page<LwgyInoutAttendance>(pageNo, pageSize);
		IPage<LwgyInoutAttendance> pageList = lwgyInoutAttendanceService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param lwgyInoutAttendance
	 * @return
	 */
	@AutoLog(value = "考勤记录-添加")
	@ApiOperation(value="考勤记录-添加", notes="考勤记录-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody LwgyInoutAttendance lwgyInoutAttendance) {
		lwgyInoutAttendanceService.save(lwgyInoutAttendance);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param lwgyInoutAttendance
	 * @return
	 */
	@AutoLog(value = "考勤记录-编辑")
	@ApiOperation(value="考勤记录-编辑", notes="考勤记录-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody LwgyInoutAttendance lwgyInoutAttendance) {
		lwgyInoutAttendanceService.updateById(lwgyInoutAttendance);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "考勤记录-通过id删除")
	@ApiOperation(value="考勤记录-通过id删除", notes="考勤记录-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		lwgyInoutAttendanceService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "考勤记录-批量删除")
	@ApiOperation(value="考勤记录-批量删除", notes="考勤记录-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.lwgyInoutAttendanceService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "考勤记录-通过id查询")
	@ApiOperation(value="考勤记录-通过id查询", notes="考勤记录-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		LwgyInoutAttendance lwgyInoutAttendance = lwgyInoutAttendanceService.getById(id);
		if(lwgyInoutAttendance==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(lwgyInoutAttendance);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param lwgyInoutAttendance
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, LwgyInoutAttendance lwgyInoutAttendance) {
        return super.exportXls(request, lwgyInoutAttendance, LwgyInoutAttendance.class, "考勤记录");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, LwgyInoutAttendance.class);
    }


	 /**
	  * 按照住宿楼宇查询最近的住宿考勤记录
	  *
	  * @param lwgyBaseBed
	  * @param pageNo
	  * @param pageSize
	  * @param req
	  * @return
	  */
	 @AutoLog(value = "考勤记录-分页考勤查询")
	 @ApiOperation(value = "考勤记录-分页考勤查询", notes = "考勤记录-分页考勤查询")
	 @GetMapping(value = "/listCustom")
	 public Result<?> queryPageListCustom(LwgyInoutAttendanceFormStu lwgyBaseBed,
										  @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
										  @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
										  @RequestParam(name = "studentName",required = false) String studentName,
										  @RequestParam(name = "studentNumber",required = false) String studentNumber,
										  @RequestParam(name = "roomSex",required = false) String roomSex,
										  @RequestParam(name = "studentCampus",required = false) String studentCampus,
										  @RequestParam(name = "studentCollege",required = false) String studentCollege,
										  @RequestParam(name = "studentMajor",required = false) String studentMajor,
										  @RequestParam(name = "studentClass",required = false) String studentClass,
										  @RequestParam(name = "studentYear",required = false) String studentYear,
										  @RequestParam(name = "studentEducation",required = false) String studentEducation,
										  @RequestParam(name = "studentState",required = false) String studentState,
										/*  @RequestParam(name = "studentSchoolSystem",required = false) String studentSchoolSystem,*/
										  @RequestParam(name = "kqTime",required = false) String kq_time,
										  /*@RequestParam(name = "studentLeaveschool_end",required = false) String studentLeaveschool_end,*/
										  HttpServletRequest req) {
		 String type = "";
		 String id = lwgyBaseBed.getId();
		 if(oConvertUtils.isEmpty(type)){
			 LwgyBaseApartment lwgyBaseApartment = lwgyBaseApartmentService.getById(id);
			 type = lwgyBaseApartment.getType();
		 }

		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 List<String> role = lwgyBaseBedService.getRoleById(sysUser.getId());
		 if (role.contains("fdy")) {//不是管理员，显示管理员分配的的床位
			 String workNo = sysUser.getWorkNo();
			 System.err.println("work"+workNo);
			 lwgyBaseBed.setAdminId(workNo);
		 }
		 if (role.contains("yanjiusheng")) {
			 String workNo = sysUser.getWorkNo();
			 System.err.println("workKKKKK"+workNo);
			 lwgyBaseBed.setAdminId(workNo);
		 }
		 if (type != null) {
			 switch (type) {
				 case "campus":
					 lwgyBaseBed.setCampusId(id);
					 break;
				 case "building":
					 lwgyBaseBed.setBuildingId(id);
					 break;
				 case "unit":
					 lwgyBaseBed.setUnitId(id);
					 break;
				 case "layer":
					 lwgyBaseBed.setLayerId(id);
					 break;
				 case "room":
					 lwgyBaseBed.setRoomId(id);
					 break;
				 default:
					 break;
			 }
		 }

		 lwgyBaseBed.setId(null);
		 lwgyBaseBed.setStudentCampus(null);
		 lwgyBaseBed.setStudentCollege(null);
		 lwgyBaseBed.setStudentYear(null);
		 lwgyBaseBed.setStudentMajor(null);
		 lwgyBaseBed.setStudentClass(null);

		 QueryWrapper<LwgyInoutAttendanceFormStu> queryWrapper = QueryGenerator.initQueryWrapper(lwgyBaseBed, req.getParameterMap());
		 Page<LwgyInoutAttendanceFormStu> page = new Page<>(pageNo, pageSize);
		 Date date = new Date();
		 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		 String format = sdf.format(date);

		 if (StringUtils.isNotBlank(roomSex)) {
			 queryWrapper.eq("t1.room_sex", roomSex);
		 }
		 if (StringUtils.isNotBlank(studentName)){
			 queryWrapper.eq("t1.student_name",studentName);
		 }
		 if (StringUtils.isNotBlank(studentNumber)){
			 queryWrapper.eq("t1.student_number",studentNumber);
		 }
		 if (StringUtils.isNotBlank(studentCampus)){
			 queryWrapper.eq("t1.student_campus",studentCampus);
		 }
		 if (StringUtils.isNotBlank(studentCollege) && !studentCollege.equals("A")){
			 queryWrapper.eq("t1.student_college",studentCollege);
		 }

		 if (StringUtils.isNotBlank(studentYear)){
			 if(studentCollege.equals("A")){
				 queryWrapper.eq("t1.year_name",studentYear);
			 } else {
				 queryWrapper.eq("t1.student_year",studentYear);
			 }
		 }

		 if (StringUtils.isNotBlank(studentMajor)){
			 queryWrapper.eq("t1.student_major",studentMajor);
		 }
		 if (StringUtils.isNotBlank(studentClass)){
			 queryWrapper.eq("t1.student_class",studentClass);
		 }
		 if (StringUtils.isNotBlank(studentEducation)){
			 queryWrapper.eq("t1.student_education",studentEducation);
		 }
		 if (StringUtils.isNotBlank(studentState)){
			 queryWrapper.eq("t1.student_state",studentState);
		 }

		 queryWrapper.eq("t1.del_flag",0);
		 queryWrapper.eq("room_type","hotel_room");
		 queryWrapper.orderByAsc("t1.room_id");

		// IPage<LwgyBaseBed> pageList2 = lwgyBaseBedService.queryPageListHotelInfo(page, queryWrapper);
		 IPage<LwgyInoutAttendanceFormStu> pageList2 = lwgyInoutAttendanceService.queryPageListKqInfo(page, queryWrapper);
		 return Result.OK(pageList2);
	 }

	 /**
	  *  编辑
	  *
	  * @param lwgyInoutAttendance
	  * @return
	  */
	 @AutoLog(value = "考勤记录-编辑处理结果")
	 @ApiOperation(value="考勤记录-编辑处理结果", notes="考勤记录-编辑处理结果")
	 @PutMapping(value = "/editResult")
	 public Result<?> editResult(@RequestBody LwgyInoutAttendance lwgyInoutAttendance) {
		 lwgyInoutAttendanceService.updateSignHandleById(lwgyInoutAttendance);
		 return Result.OK("编辑成功!");
	 }

	 @AutoLog(value = "考勤记录-通过学号获取学生轨迹")
	 @ApiOperation(value="考勤记录-通过学号获取学生轨迹", notes="考勤记录-通过学号获取学生轨迹")
	 @GetMapping(value = "/qyeryQjByStuNum")
	 public Result<?> qyeryQjByStuNum(String studentNumber) {
	 	//String stuNumber = lwgyInoutAttendance.getStudentNumber();
		List<LwgyInoutAttendance> list =  lwgyInoutAttendanceService.queryKqByStuNumber(studentNumber);
		 return  Result.OK(list);
	 }


}
